<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\index\model;

use think\facade\Db;
use app\common\model\Common;

class Message extends Common
{
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableData($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $user_id    = $post['user_id'];
        $where =[];
        if(isset($post['title']) && !empty($post['title'])){
            $where[] = ['title', 'like', '%'.$post['title'].'%'];
        }
        if(isset($post['desc']) && !empty($post['desc'])){
            $where[] = ['desc', 'like', '%'.$post['desc'].'%'];
        }
        $list = Db::name('message')
            ->order('is_top desc,ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();
        $list_data = [];
        if($list){
            $list_data = $list['data'];
            foreach ($list_data as $k => &$v){
                $user_array = $v['user_array'];
                if(!empty($user_array)){
                    $user_array = explode(',' ,$user_array);
                    if(in_array($user_id , $user_array)){
                        $v['msg_look'] = 1;
                    }else{
                        $v['msg_look'] = 0;
                    }
                }else{
                    $v['msg_look'] = 0;
                }

            }
        }
        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list_data,
            'count' => $list['total'],
        ];
    }


}
